#!/bin/bash
# 数据库导入脚本

echo "=========================================="
echo "员工事务管理系统 - 数据库初始化"
echo "=========================================="
echo ""

# 检查MySQL是否运行
if ! pgrep -x "mysqld" > /dev/null; then
    echo "❌ MySQL服务未运行，请先启动MySQL服务"
    echo "macOS启动命令: brew services start mysql 或 sudo /usr/local/mysql/support-files/mysql.server start"
    exit 1
fi

echo "✅ MySQL服务正在运行"
echo ""

# 提示输入MySQL密码
echo "请输入MySQL root用户密码（如果无密码直接回车）:"
read -s MYSQL_PWD

if [ -z "$MYSQL_PWD" ]; then
    echo "尝试无密码连接..."
    mysql -u root < sql/init.sql
    mysql -u root ygswglxt < sql/notification.sql 2>/dev/null || echo "通知表可能已存在，跳过..."
else
    echo "使用密码连接..."
    mysql -u root -p"$MYSQL_PWD" < sql/init.sql
    mysql -u root -p"$MYSQL_PWD" ygswglxt < sql/notification.sql 2>/dev/null || echo "通知表可能已存在，跳过..."
fi

if [ $? -eq 0 ]; then
    echo ""
    echo "✅ 数据库初始化成功！"
    echo ""
    echo "已创建数据库: ygswglxt"
    echo "已创建表: sys_user, attendance_record, leave_record, salary_record, notification"
    echo "已插入测试数据: 4个测试用户"
    echo ""
    echo "测试账号："
    echo "  - 员工: employee / 123456"
    echo "  - 经理: manager / 123456"
    echo "  - HR: hr / 123456"
    echo "  - 其他: other / 123456"
else
    echo ""
    echo "❌ 数据库初始化失败，请检查："
    echo "  1. MySQL服务是否运行"
    echo "  2. root用户密码是否正确"
    echo "  3. 是否有创建数据库的权限"
    exit 1
fi

